package com.dsx.myblog.web;

import com.dsx.myblog.entity.User;
import com.dsx.myblog.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;
/**
 *
 * 用户控制器
 * @author dsx
 */
@Controller
@RequestMapping(value="/users")
public class UserController {

    @Autowired
    UserService userService;

    @ResponseBody
    @ApiOperation(value="分页获取用户列表", notes="")
    @RequestMapping(value = "/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public PageInfo getUserListByPage(@PathVariable int pageNum, @PathVariable int pageSize) {
        List<User> users = userService.list(pageNum, pageSize);
        return new PageInfo(users);
    }
    @ResponseBody
    @ApiOperation(value="添加用户", notes="")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public void postUser(@RequestBody User user) {
        userService.add(user);
    }
    @ResponseBody
    @ApiOperation(value="根据id获取用户", notes="")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User getUser(@PathVariable Long id) {
        return userService.get(id);
    }



    @ResponseBody
    @ApiOperation(value="判断用户名是否重复", notes="")
    @RequestMapping(value = "/judge/{username}", method = RequestMethod.GET)
    public User getUserByName(@PathVariable String username) {
        return userService.getUserByName(username);
    }
    @ResponseBody
    @ApiOperation(value="更新用户", notes="")
    @RequestMapping(value = "/", method = RequestMethod.PUT)
    public void putUser(@RequestBody User user) {
        userService.update(user);
    }

    @ResponseBody
    @ApiOperation(value="删除用户", notes="test")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable Long id) {
        userService.delete(id);
    }

}